package com.cloudkinto.common.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author LIUJUNWU
 * @date 2019/7/2
 * @desc
 * 据生日计算年龄(年、月、日)                      
 */
public class BirthdayConvert {
//	public static void main(String[] args) {
//		String start = "2018-02-08";
//
//		// 使用SimpleDateFormat进行转换
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		try {
//			Birthday birthday = countAge(sdf.parse(start),new Date());
//			System.out.println("年=" + birthday.getYear());
//			System.out.println("月=" + birthday.getMonth());
//			System.out.println("日=" + birthday.getDay());
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

	/**
	 *   @Title：countAge      
	 *   @Description：根据生日计算年龄(年、月、日)      
	 *   @param from  生日    
	 *   @return String 返回类型      
	 */
	public static Birthday countAge(Date birthdayDate, Date abortDate) {
		String age = "";
		int year = 0;
		int month = 0;
		int day = 0;
		if (birthdayDate == null) {
			age = age + 0;
		} else {
			Calendar c1 = Calendar.getInstance();
			Calendar c2 = Calendar.getInstance();
			c1.setTime(birthdayDate);
			c2.setTime(abortDate);
			if (c1.after(c2)) {
				throw new IllegalArgumentException("生日不能超过当前日期");
			}
			int from_year = c1.get(Calendar.YEAR);
			int from_month = c1.get(Calendar.MONTH) + 1;
			int from_day = c1.get(Calendar.DAY_OF_MONTH);
//			System.out.println("以前：" + from_year + "-" + from_month + "-" + from_day);
			int MaxDayOfMonth = c1.getActualMaximum(Calendar.DAY_OF_MONTH);
			// System.out.println(MaxDayOfMonth);
			int to_year = c2.get(Calendar.YEAR);
			int to_month = c2.get(Calendar.MONTH) + 1;
			int to_day = c2.get(Calendar.DAY_OF_MONTH);
//			System.out.println("现在：" + to_year + "-" + to_month + "-" + to_day);

			year = to_year - from_year;
			if (c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR) < 0) {
				year = year - 1;
			}
			// if(year < 1){// 小于一岁要精确到月份和天数
//			System.out.println("--------");
			if (to_month - from_month > 0) {
				month = to_month - from_month;
				if (to_day - from_day < 0) {
					month = month - 1;
					day = to_day - from_day + MaxDayOfMonth;
				} else {
					day = to_day - from_day;
				}
			} else if (to_month - from_month == 0) {
				if (to_day - from_day > 0) {
					day = to_day - from_day;
				}
			}
			// }
			if (year > 1) {
				age = age + year + "岁";
			} else if (month > 0) {
				age = age + month + "个月" + day + "天";
			} else {
				age = age + day + "天";
			}
		}
		Birthday birthday = new Birthday();
		birthday.year = year;
		birthday.month = month;
		birthday.day = day;
		
//		System.out.println(birthday.year + "--" + birthday.month + "--" + birthday.day);
		return birthday;
	}
	
    
   public static class Birthday{
		public int year;
		public int month;
		public int day;
		public int getYear() {
			return year;
		}
		public void setYear(int year) {
			this.year = year;
		}
		public int getMonth() {
			return month;
		}
		public void setMonth(int month) {
			this.month = month;
		}
		public int getDay() {
			return day;
		}
		public void setDay(int day) {
			this.day = day;
		}
    }
	
}
